package com.anydata.android.weatherforecast.weather.shell;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.widget.Toast;

import com.anydata.android.weatherforecast.weather.util.CityListPropertyUtil;
import com.anydata.android.widget.weatherclock.AutoFindLocationService;
import com.anydata.android.widget.weatherclock.UpdateService;
import com.anydata.android.widget.weatherclock.WeatherForecastAppWidget;
import com.anydata.android.R;

public class WeatherPreference extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	public static final String KEY_AUTO_UPDATE_PREF = "autoUpdate";
	public static final String KEY_TEMP_UNIT_PREF = "PrefTempFmt";
	public static final String KEY_UPDATE_FREQ_PREF = "update_interval";
	public static final String KEY_CITY_LIST_PREF = "cityList";
	public static final String KEY_CLOCK_MODE_PREF = "clockmode";
	
	public static final String CITY_ITEM = "city_items";
	public static final String CITY_LIST_NAME = "city_list_name";
	
	public static final String DEFAULT_CITY_ITEM_VALUE = 
		"北京,上海,天津 ,重庆,唐山,石家庄,大连,哈尔滨,海口,长春,长沙,成都,福州,广州,贵阳,杭州,合肥,呼和浩特,济南,昆明,拉萨,兰州,南昌,南京,南宁,青岛,深圳,沈阳,太原,乌鲁木齐,武汉,西安,西宁,厦门,徐州,银川,郑州,高雄";
	public static final String DEFAULT_CITY_LIST_NAME_VALUE = 
		"北京,上海,天津 ,重庆,唐山,石家庄,大连,哈尔滨,海口,长春,长沙,成都,福州,广州,贵阳,杭州,合肥,呼和浩特,济南,昆明,拉萨,兰州,南昌,南京,南宁,青岛,深圳,沈阳,太原,乌鲁木齐,武汉,西安,西宁,厦门,徐州,银川,郑州,高雄";
	private CheckBoxPreference mAutoUpdatePref;
	private ListPreference mTempertureUnitPref;
	private ListPreference mUpdateFrequencePref;
	private CityListPreference mCityListPref;
	private ListPreference mClockModePref;

	public static WeatherPreference wp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.weathersetting);

		
//		mAutoUpdatePref = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_AUTO_UPDATE_PREF);
		mTempertureUnitPref = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_TEMP_UNIT_PREF);
		mUpdateFrequencePref = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_UPDATE_FREQ_PREF);
		mCityListPref = (CityListPreference) getPreferenceScreen().findPreference(
				KEY_CITY_LIST_PREF);
		mClockModePref = (ListPreference) getPreferenceScreen().findPreference(KEY_CLOCK_MODE_PREF);
		
		wp = this;
		//load data for mCityListPref from properties file
		CityListPropertyUtil.loadCityListForPreferences(mCityListPref, this);
		
		
//		if(mAutoUpdatePref.isChecked()){
//			System.out.println("AAAAAAAAAAAAA");
//			
//			/*Intent serviceIntent = new Intent();
//			serviceIntent.setAction("com.anydata.android.widget.weatherclock.AutoFindLocationService");
//			startService(serviceIntent);*/
//			
//			Context c = this.getApplicationContext();
//			Intent i = new Intent(c, AutoFindLocationService.class);
//			i.setAction(UpdateService.ACTION_UPDATE_ALL);
//			c.startService(new Intent(c, AutoFindLocationService.class));
//			
//		}else{
//			System.out.println("BBBBBBBBBBB");
//			Context c = this.getApplicationContext();
//			Intent i = new Intent(c, AutoFindLocationService.class);
//			i.setAction(UpdateService.ACTION_UPDATE_ALL);
//			c.stopService(new Intent(c, AutoFindLocationService.class));
//		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Set up a listener whenever a key changes
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();

		// Unregister the listener whenever a key changes
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);

	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// Let's do something when my counter preference value changes
		if (key.equals(KEY_AUTO_UPDATE_PREF)) {
			Toast.makeText(
					this,
					"Thanks! You changed Auto Update to "
							+ sharedPreferences.getBoolean(key, Boolean.TRUE),
					Toast.LENGTH_SHORT).show();
			
			if(mAutoUpdatePref.isChecked()){
				System.out.println("onSharedPreferenceChanged - AAAAAAAAAAAAA");
				
				Context c = this.getApplicationContext();
				Intent i = new Intent(c, AutoFindLocationService.class);
				i.setAction(UpdateService.ACTION_UPDATE_ALL);
				c.startService(new Intent(c, AutoFindLocationService.class));
				
			}else{
				System.out.println("onSharedPreferenceChanged - BBBBBBBBBBB");
				Context c = this.getApplicationContext();
				Intent i = new Intent(c, AutoFindLocationService.class);
				i.setAction(UpdateService.ACTION_UPDATE_ALL);
				c.stopService(new Intent(c, AutoFindLocationService.class));
			}
		} else if (key.equals(KEY_CITY_LIST_PREF)) {
			Toast.makeText(
					this,
					"You changed City to "
							+ sharedPreferences.getString(key, "not changed."),
					Toast.LENGTH_SHORT).show();
		} else if (key.equals(KEY_TEMP_UNIT_PREF)) {
			Toast.makeText(
					this,
					"You changed Temperture Unit to "
							+ sharedPreferences.getString(key, "not changed."),
					Toast.LENGTH_SHORT).show();
		} else if (key.equals(KEY_UPDATE_FREQ_PREF)) {
			Toast.makeText(
					this,
					"You changed Update Frequency to "
							+ sharedPreferences.getString(key, "not changed."),
					Toast.LENGTH_SHORT).show();
		} else if( key.equals(KEY_CLOCK_MODE_PREF)){
			Toast.makeText(this, "Thanks! You changed Clock Mode to " + sharedPreferences.getString(key, "used one."), Toast.LENGTH_SHORT).show();
		}
		//need call the update services.
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		Intent widgetUpdate = new Intent();
        widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(this,
				WeatherForecastAppWidget.class));
        widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        widgetUpdate.setClass(this, WeatherForecastAppWidget.class);
        
        sendBroadcast(widgetUpdate);
	}
}